using System;
using System.Xml;
using System.Collections.Generic;

namespace Dida
{
	[Dxml.DidaObject]
	public class AniSet : IAniObject
	{
		List<IAniObject> aniObjects = null;
		bool enabled = true;

		internal AniSet(object[] aniObjs)
		{
			aniObjects = new List<IAniObject>(aniObjs.Length);
			for (int i = 0; i < aniObjs.Length; i++)
				aniObjects.Add((IAniObject)aniObjs[i]);
		}

		public void Start()
		{
			foreach (IAniObject aniObj in aniObjects)
				aniObj.Start();
		}

		public void Update(float timeDelta)
		{
			if (enabled)
			{
				foreach (IAniObject aniObj in aniObjects)
					aniObj.Update(timeDelta);
			}
		}

		public void Enable()
		{
			enabled = true;
		}

		public void Disable()
		{
			enabled = false;
		}

		public void ToggleEnabled()
		{
			enabled = !enabled;
		}
	}

	public class AniSetCompiler : Dxml.ContainerObjectCompilerBase
	{
		List<Dxml.CompilingObject> aniObjects = new List<Dxml.CompilingObject>();

		public override void Load(XmlReader reader, Dxml.XmlCompiler compiler)
		{
			ReadDefAttributes(reader);
			compiler.LoadContainerChildren(reader, this);
		}

		public override Dxml.CompilingReference[] GetReferences()
		{
			Dxml.CompilingReference[] ret = new Dida.Dxml.CompilingReference[aniObjects.Count];
			for (int i = 0; i < aniObjects.Count; i++)
			{
				ret[i] = aniObjects[i].Reference;
			}
			return ret;
		}

		public override object CreateObject(Dxml.XmlCompiler compiler, object[] resolvedReferences)
		{
			return new AniSet(resolvedReferences);
		}

		public override Type ObjectType
		{
			get { return typeof(AniSet); }
		}

		public override Dxml.ReferencesOrder ReferencesOrder
		{
			get
			{
				return Dxml.ReferencesOrder.DependenciesOrder;
			}
		}

		public override Dxml.ContainerAddResult AddChild(Dida.Dxml.CompilingObject child)
		{
			if (typeof(IAniObject).IsAssignableFrom(child.ObjectCompiler.ObjectType))
			{
				aniObjects.Add(child);
				return Dxml.ContainerAddResult.Added;				
			}
			else
				return Dxml.ContainerAddResult.NotAdded;
		}
	}
}